package com.googlecode.stockhamster.arbiter;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Account {
	private String username;
	private String password;
	private String bankCode;
	private String stockCode;

	private static PreparedStatement state = null;
	private static ResultSet rs = null;

	public static Account get(String username) {
		try {
			String sqlGetAccount = "select * from ArbiterAccount where username=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccount);
			state.setString(1, username);
			rs = state.executeQuery();
			if (rs.next()) {
				Account account = new Account();
				account.setUsername(rs.getString(1));
				account.setPassword(rs.getString(2));
				account.setBankCode(rs.getString(3));
				account.setStockCode(rs.getString(4));
				state.close();
				rs.close();
				return account;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (state != null)
					state.close();
				if (rs != null)
					rs.close();
			} catch (SQLException e) {
			}
		}
		return null;
	}

	public static Account[] getAll() {
		ArrayList<Account> tempAccount = new ArrayList<Account>();
		try {
			String sqlGetAccountList = "select * from ArbiterAccount";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccountList);
			rs = state.executeQuery();
			while (rs.next()) {
				Account ac = new Account();
				ac.setUsername(rs.getString(1));
				ac.setPassword(rs.getString(2));
				ac.setBankCode(rs.getString(3));
				ac.setStockCode(rs.getString(4));
				tempAccount.add(ac);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (state != null)
					state.close();
				if (rs != null)
					rs.close();
			} catch (SQLException e) {
			}
		}
		Account[] target = new Account[tempAccount.size()];
		for (int i = 0; i < target.length; i++)
			target[i] = tempAccount.get(i);
		return target;
	}

	public boolean save() {
		try {
			String sqlSetArbiterAccount = "update ArbiterAccount set password=?, bankcode=?, stockcode=? where username=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlSetArbiterAccount);
			state.setString(4, getUsername());
			state.setString(1, getPassword());
			state.setString(2, getBankCode());
			state.setString(3, getStockCode());
			state.executeUpdate();
			state.close();
			return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null)
					state.close();
			} catch (SQLException e) {
			}
		}
		return false;
	}

	public boolean insert() {
		try {
			String sqlAddAmount = "insert into ArbiterAccount(username, password, bankcode, stockcode) values(?,?,?,?)";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlAddAmount);
			state.setString(1, username);
			state.setString(2, password);
			state.setString(3, bankCode);
			state.setString(4, stockCode);
			state.executeUpdate();
			state.close();
			return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null)
					state.close();
			} catch (SQLException e) {
			}
		}
		return false;
	}

	public boolean delete() {
		try {
			String sqlDelAccount = "delete from ArbiterAccount where username=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlDelAccount);
			state.setString(1, username);
			state.executeUpdate();
			state.close();
			return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null)
					state.close();
			} catch (SQLException e) {
			}
		}
		return false;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getBankCode() {
		return bankCode;
	}

	public void setBankCode(String bankCode) {
		this.bankCode = bankCode;
	}

	public String getStockCode() {
		return stockCode;
	}

	public void setStockCode(String stockCode) {
		this.stockCode = stockCode;
	}
}
